package logic

import (
	"context"
	"errors"
	"goods/goods"
	"goods/internal/svc"
	"goods/models/mysqlinit"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodLogic {
	return &CreateGoodLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGoodLogic) CreateGood(in *goods.CreateGoodsRequest) (*goods.CreateGoodsResponse, error) {
	// todo: add your logic here and delete this line
	g := mysqlinit.Goods{

		GoodsTitle:  in.GoodsTitle,
		GoodsPrice:  float64(in.GoodsPrice),
		GoodsStock:  int(in.GoodsStock),
		GoodsStatus: int(in.GoodsStatus),
	}
	err := g.CreateGoods()
	if err != nil {
		logx.Error("商品发布失败")
		return nil, errors.New("商品发布失败")
	}

	return &goods.CreateGoodsResponse{
		Id: int64(g.ID),
	}, nil
}
